package Derivation;

import java.util.List;

import Basics.Atom;
import Basics.Program;
import Basics.Rule;

public class DeriveByProgramTopDown extends DeriveByProgram 
{
	
	
	public DeriveByProgramTopDown (Rule ... irs)
	{
		super(irs);
	}
	
	
	
	public DeriveByProgramTopDown (Program p)
	{
		super(p);
	}

	
	
	/*************************************************************************************************************/
	/** Title: GetDerivationsForAtomTopDown																				
	/** Description: Finds all Derivations for specific atom 				
	/*************************************************************************************************************/
	
	public void GetDerivationsForAtomTopDown (Atom partlyInstAtom)
	{
		for (Rule rule : this.p.getRules()) 
		{
			if (rule.getHead().getName().equals(partlyInstAtom.getName())) 
			{
				Rule copyOfRule = new Rule (rule);
				copyOfRule.SwapToInstAtomAndPartiallyInst(copyOfRule.getHead(), partlyInstAtom);
				DeriveByRuleTopDown ruleDeriver = new DeriveByRuleTopDown(copyOfRule);//, this.db
				ruleDeriver.FindDerivationsForRuleTopDown();
				this.derivationsVec.add(ruleDeriver.getDerivations());
			}
		}
	}
	
	
	
	/*************************************************************************************************************/
	/** Title: IsFullyInstList																				
	/** Description: Check if the list of rules contains only fully inst rules  				
	/*************************************************************************************************************/
	
	public boolean IsFullyInstList (List<Rule> instRuleVector)
	{
		boolean fullyInstList = true;
		for (Rule rule : instRuleVector) 
		{
			if (false == rule.isFullyInst()) 
			{
				fullyInstList = false;
			}
		}
		
		return fullyInstList;
	}
}
